package com.keli.dt.model.bpss;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;

import com.keli.dt.model.LaneReport;

public class LaneReportInsertBPSS implements BatchPreparedStatementSetter {
	private List<LaneReport> laneReports;
	private String reportTime;
	
	public LaneReportInsertBPSS(List<LaneReport> laneReports, String reportTime) {
		this.laneReports = laneReports;
		this.reportTime = reportTime;
	}
	
	public static String insertHistorySql() {
		return insertSql("DT_LANE_REPORT_H", "LAN_H_SEQ.NEXTVAL");
	}
	
	public static String insertRealTimeSql() {
		return insertSql("DT_LANE_REPORT_R", "LAN_R_SEQ.NEXTVAL");
	}
	
	public static String insertSql(String tableName, String seq) {
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO ");
		sql.append(tableName);
		sql.append("(ID, DETECT_CLASS, DETECT_ID, ROAD_ID, LANE, VEHICLE_CLASS, ");
		sql.append("SPEED, DENSITY, VOLUME, CYCLE, COLLEC_TIME, COLLEC_DATETIME, REPORT_TIME) VALUES(");
		sql.append(seq);
		sql.append(", ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, TO_DATE(?, 'YYYY/MM/DD HH24:MI:SS'), TO_DATE(?, 'YYYY/MM/DD HH24:MI:SS'))");

		return sql.toString();
	}
	
	@Override
	public int getBatchSize() {
		return laneReports.size();
	}

	@Override
	public void setValues(PreparedStatement ps, int index) throws SQLException {
		LaneReport lr = laneReports.get(index);
		int i = 1;
		ps.setString(i++, lr.getDETECT_CLASS());
		ps.setString(i++, lr.getDETECT_ID());
		ps.setBigDecimal(i++, lr.getROAD_ID());
		ps.setString(i++, lr.getLANE());
		ps.setString(i++, lr.getVEHICLE_CLASS());
		ps.setBigDecimal(i++, lr.getSPEED());
		ps.setString(i++, lr.getDENSITY());
		ps.setBigDecimal(i++, lr.getVOLUME());
		ps.setBigDecimal(i++, lr.getCYCLE());
		ps.setString(i++, lr.getCOLLEC_TIME());
		ps.setString(i++, lr.getCOLLEC_DATETIME());
		ps.setString(i++, reportTime);
	}

}
